var prefix = "/biz/software"

$().ready(function() {
	validateRule();
});
$(function() {

	var tag_data = [
		{id:1 ,name:'Chicago Bulls',desc:'芝加哥公牛'},
		{id:2 ,name:'Cleveland Cavaliers',desc:'克里夫兰骑士'},
		{id:3 ,name:'Detroit Pistons',desc:'底特律活塞'},
		{id:4 ,name:'Indiana Pacers',desc:'印第安纳步行者'}
	];
//showField：设置下拉列表中显示文本的列
//keyField：设置下拉列表项目中项目的KEY值，用于提交表单
//data：数据源，可以是JSON数据格式，也可以是URL
	$('#selectPage').selectPage({
		showField : 'desc',
		keyField : 'id',
		data : tag_data
	});
//获得选中项目的文本内容
	$('#func1').click(function(){
		alert($('#selectPage').selectPageText());
	});
//清除所有选中的项目
	$('#func2').click(function(){
		$('#selectPage').selectPageClear();
	});
//动态修改选中项目
	$('#func3').click(function(){
		$('#selectPage').val('20');
		$('#selectPage').selectPageRefresh();
	});
//设置插件禁用 / 启用
	$('#funcDisabled').click(function(){
		if($('#selectPage').selectPageDisabled())//判断当前状态
			$('#selectPage').selectPageDisabled(false);
		else
			$('#selectPage').selectPageDisabled(true);
	});

	load();
});



$.validator.setDefaults({
	submitHandler : function() {
		submit();
	}
});
function load(){
	var myDate = new Date();

	year=myDate.getFullYear();    //获取完整的年份(4位,1970-????)
	month=myDate.getMonth()+1;       //获取当前月份(0-11,0代表1月)
	day=myDate.getDate();        //获取当前日(1-31)
	var currDate=year+'-'+month+'-'+day;
	document.getElementById("submitDate").value=currDate;

	workDay=31;
	console.log(workDay+'days');
	weeks=Math.floor(workDay/5);

	days=parseInt(workDay)+weeks*2;
	console.log(days);
	let date=new Date();
	date.setDate(date.getDate()+days);

	if(date.getDay()==6||date.getDay()==0){
		date.setDate(date.getDate()+2);
	}
	document.getElementById("approvalDate").value=formatDate(date,0);


}
function reload() {
	var sn=document.getElementById('ssn').value;
	var approvalDate=document.getElementById('approvalDate').value;
	sn=sn+','+approvalDate;
	var index=parent.layer.open({
		type: 2,
		title: '编辑',
		maxmin: true,
		shadeClose: false,
		area: ['800px', '520px'],
		content: prefix + '/reload/' + sn
	});
	parent.layer.full(index);

}
function formatDate(d,format) {
	// 如果date不是日期对象，返回
	if(!(d instanceof Date)) {
		return;
	}
	var year = d.getFullYear();    // 获取年
	var month = d.getMonth() + 1 ; // 月份是从0开始
	var day = d.getDate();         // 获取日
	var hours = d.getHours();      // 获取时间 0~23
	var minutes = d.getMinutes();  // 获取分钟
	var seconds = d.getSeconds();  // 获取秒数
	// 如果小于10 数据要补充0
	// 写一个函数是 yyyy-MM-dd HH:mm:ss(优化后)
	month < 10 ? month = "0"+month: month;
	day < 10 ? day = "0"+day:day;
	hours < 10 ? hours = "0"+ hours: hours;
	minutes < 10 ? minutes = "0"+ minutes: minutes;
	seconds < 10 ? seconds = "0"+ seconds: seconds;
	if(format==0){return year+"-"+month+"-"+day;}
	if(format==1){return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;}


}

function save() {
	$('#status').val(0);
	$.ajax({
		cache : true,
		type : "POST",
		url : "/biz/software/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				$('#sn').empty();
				$("#sn").focusin;
				parent.layer.msg("保存成功");
				parent.reLoad();
				var ctrl=document.getElementById("softwareName");
				ctrl.value="";
				ctrl=document.getElementById("sn");
				ctrl.value="";
				ctrl.focus();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);


			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}



function submit() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/biz/software/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				$('#sn').empty();
				$("#sn").focusin;
				parent.layer.msg("提交成功");
				parent.reLoad();
				var ctrl=document.getElementById("softwareName");
				ctrl.value="";
				ctrl=document.getElementById("sn");
				ctrl.value="";
				ctrl.focus();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);


			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			sn : {required : true},
			copyrightOwner : {required : true},
			softwareName : {required : true},
			businessMan : {min : 0},
			approvalDate : {required : true},
			type : {required : true},
			agentCompany : {required : true}
		},
		messages : {
			sn             : {required : icon + "请输入流水号"},
			copyrightOwner : {required : icon + "请输入著作权人"},
			softwareName   : {required : icon + "请输入软件名称"},
			businessMan    : {min : icon + "请选择业务员"},
			approvalDate   : {required : icon + "请输入发证日期"},
			type           : {required : icon + "请选择是否写材料"},
			agentCompany   : {required : icon + "请输入代理公司"}
		}
	})
}